package com.ct.ah.lab.service.impl;

import com.alibaba.fastjson.JSON;
import com.ct.ah.lab.dao.OrderDao;
import com.ct.ah.lab.dao.PointDao;
import com.ct.ah.lab.dao.PointItemDao;
import com.ct.ah.lab.entity.Order;
import com.ct.ah.lab.entity.Point;
import com.ct.ah.lab.entity.PointItem;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.entity.vo.PointItemVo;
import com.ct.ah.lab.service.IOrderService;
import com.ct.ah.lab.service.IPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.List;

@Service("PointService")
public class PointServiceImpl implements IPointService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private PointDao pointDao;
    @Autowired
    private PointItemDao pointItemDao;
    @Autowired
    private IOrderService orderService;

    //根据一个订单保存一条积分记录
    @Override
    public void savePointItem(Integer oid) {
        Order order = orderDao.getByOid(oid);
        //首先---获取订单信息---订单的价格、和用户
        //查询到该用户的当前point信息---获取用户当前积分数，根据价格求积分规则，然后将更新用户总积分，
        //同时生产一条记录插入到子积分表中
        //获取当前用户的积分
        int count = pointDao.getCountByUid(order.getUid());
        if (count == 0) {
            //如果没有积分过，新增
            this.savePoint(order);
        } else {
            //如果存在，进行修改操作
            this.updatePoint(order);
        }

        //生成积分逻辑，10块钱就是一个积分
        PointItem pointItem = new PointItem();
        pointItem.setUid(order.getUid());
        pointItem.setOid(order.getId());
        pointItem.setChangeNum(order.getPrice() / 10);
        pointItem.setRemainNum(pointDao.getByUid(order.getUid()).getNum());// + order.getPrice()
        pointItem.setReason(2);
        pointItem.setCreatedUser(order.getCreatedUser());
        pointItemDao.save(pointItem);
    }

    //列出所有积分条目
    @Override
    public List<PointItemVo> listPointItem(User user) {
        //格式化时间工具
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        //获取所有的积分记录
        List<PointItem> pointItemList = pointItemDao.getByUid(user.getUid());
        //将每个积分记录与产生的订单信息关联起来
        List<PointItemVo> pointItemVoList = JSON.parseArray(JSON.toJSONString(pointItemList), PointItemVo.class);
        for (PointItemVo pointItemVo :
                pointItemVoList) {
            pointItemVo.setPayTime(sdf.format(pointItemVo.getCreatedTime()));
            Order order = orderService.getbyOid(pointItemVo.getOid());
            pointItemVo.setPointItemOrder(order);
            pointItemVo.setUser(user);
        }
        return pointItemVoList;
    }

    @Override
    public void savePoint(Order order) {
        Point point = new Point();
        point.setUid(order.getUid());
        //积分规则，每消费10元积累1分
        Double num = order.getPrice() / 10;
        point.setNum(num);
        pointDao.save(point);
    }

    @Override
    public void updatePoint(Order order) {
        Point point = this.getPoint(order.getUid());
        //积分规则，每消费10元积累1分
        Double newNum = point.getNum() + (order.getPrice() / 10);
        point.setNum(newNum);
        pointDao.update(point);
    }

    @Override
    public void checkPointAdd(Double iptNum, Integer uid) {

        int count = pointDao.getCountByUid(uid);
        if (count == 0) {
            Point point = new Point();
            point.setUid(uid);
            point.setNum(iptNum);
            pointDao.save(point);
        } else {
            Point point = this.getPoint(uid);
            //积分规则，每消费10元积累1分
            Double newNum = point.getNum() + iptNum;
            point.setNum(newNum);
            pointDao.update(point);
        }




    }

    @Override
    public Point getPoint(Integer uid) {
//        List<PointItem> pointItems = pointItemDao.getByUid(uid);
//        Long totalPointNum = 0L;
//        for(PointItem pointItem: pointItems){
//            totalPointNum += pointItem.getChangeNum();
//        }
//        Point point = new Point();
//        point.setUid(uid);
//        point.setNum(totalPointNum);
//        return point;
        return pointDao.getByUid(uid);
    }

    //使用积分进行消费
    @Override
    public void reducePoint(Integer oid, User user) {
        Order order = orderDao.getByOid(oid);
        Double consumePoint = order.getPrice();
        //根据当前用户id获取用户积分
        Point point = pointDao.getByUid(user.getUid());
        //变更用户积分
        point.setNum(point.getNum() - consumePoint);
        pointDao.updateById(point);
        //生成消费记录
        PointItem pointItem = new PointItem();
        pointItem.setUid(order.getUid());
        pointItem.setOid(order.getId());
        pointItem.setChangeNum((order.getPrice()));
        pointItem.setRemainNum(pointDao.getByUid(order.getUid()).getNum());
        pointItem.setReason(1);
        pointItem.setCreatedUser(order.getCreatedUser());
        pointItemDao.save(pointItem);

        //修改订单状态--待收货2 积分支付2
        orderService.changeStatus(oid, 2, 2, user);
    }

    //积分退货
    @Override
    public void returnPoint(Integer oid, User user) {
        Order order = orderDao.getByOid(oid);
        Double consumePoint = order.getPrice();

        //根据当前用户id获取用户积分
        Point point = pointDao.getByUid(user.getUid());
        //变更用户积分
        point.setNum(point.getNum() + consumePoint);
        pointDao.updateById(point);
        //生成消费记录
        PointItem pointItem = new PointItem();
        pointItem.setUid(order.getUid());
        pointItem.setOid(order.getId());
        pointItem.setChangeNum((order.getPrice()));
        pointItem.setRemainNum(pointDao.getByUid(order.getUid()).getNum());
        pointItem.setReason(3);
        pointItem.setCreatedUser(order.getCreatedUser());
        pointItemDao.save(pointItem);

        //修改订单状态--待收货2 积分支付2
        orderService.changeStatus(oid, 4, 4, user);
    }

}
